//
//  YFBottomBar.swift
//  微博
//
//  Created by 余仿 on 16/4/21.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

let baseTag2 = 200

class YFBottomBar: UIStackView {


    //MARK:记录被点击的按钮
    private var previousBtn:UIButton?

    //MARK:按钮点击的回调
    var btnDidSelected:((index:Int) -> ())?

    init(){
        super.init(frame: CGRectZero)
        setupUI()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func setupUI(){

        //创建三个按钮
        let leftBtn = UIButton(imageName: "compose_emotion_table_left", title: "默认", target: self, action: "btnDidClick:")
        leftBtn.tag = 0 + baseTag

        let midBtn = UIButton(imageName: "compose_emotion_table_mid", title: "Emoji", target: self, action: "btnDidClick:")
        midBtn.tag = 1 + baseTag

        let rightBtn = UIButton(imageName: "compose_emotion_table_right", title: "浪小花", target: self, action: "btnDidClick:")
        rightBtn.tag = 2 + baseTag

        addArrangedSubview(leftBtn)
        addArrangedSubview(midBtn)
        addArrangedSubview(rightBtn)

        //设置stackView的属性
        self.axis = .Horizontal
        self.distribution = .FillEqually

        //初始化的时候默认选中第一个
        btnDidClick(leftBtn)

        //设置tag
    }

    //MARK:底部条工具条按钮的点击方法
    func btnDidClick(btn:UIButton){

        print("设置选中状态")

        previousBtn?.selected = false

        btn.selected = true
        
        previousBtn = btn
        
        //选中对应的视图
        //调用回调闭包
        btnDidSelected?(index: btn.tag)
    }
    
    func setBtnSelected(index:Int){
        
        //根据索引获取按钮
        let btn = viewWithTag(index + baseTag) as! UIButton
        
        previousBtn?.selected = false
        
        btn.selected = true
        
        previousBtn = btn
        
    }
}

